<template>
  <view class="pay">
    <!-- 1 收货地址 -->
    <view class="address u-p-20">
      <view class="address-btn u-flex u-row-center">
        <view v-if="address" class="address-detail">
        <view class="address-detail">{{addressDetail}}</view>
        <view class="address-user"><text>{{address.userName}}</text><text>{{address.telNumber}}</text></view>
      </view>
        <u-button
          v-else
          shape="circle"
          type="error"
          plain
          size="medium"
          :custom-style="{ backgroundColor: '#fff!important' }"
          @click="handleChooseAddress"
          >获取收货地址</u-button
        >
      </view>
    </view>

    <u-line color="#eee"></u-line>

    <!-- 2 已选商品 -->
    <view class="goods-list u-p-15">
      <view class="goods-list-title">已选商品</view>
      <view class="goods-list-content">
        <view v-for="item in checkedCarts" :key="item.goods_id" class="cart-item" >
          <!-- 2 图片 -->
          <view class="goods-img">
            <u-image :src="item.goods_small_logo" width="191rpx" height="191rpx"></u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2">{{item.goods_name}}</view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price">￥{{item.goods_price}}</view>
              <view class="goods-num-tool">
                <view class="goods-vnum">X{{item.num}}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-price"> 合计: <text>￥{{togglGoodsprice}}</text> </view>
      <view class="all-count">
        <u-button type="error" shape="circle" @click="handlePay">去支付({{togglGoodsNums}})</u-button>
      </view>
    </view>
  </view>
</template>

<script>
import {mapGetters} from 'vuex'
export default {
  
    data(){
        return{
            address:null
        }
    },
    computed:{
        ...mapGetters('cart',['checkedCarts','togglGoodsprice','togglGoodsNums']),
           // 地址详情
        addressDetail(){
            if(!this.address) return ""
            const { provinceName, cityName, countyName, detailInfo } = this.address;
            return provinceName+cityName+countyName+detailInfo
        }
    },
    methods:{
        // 获取收货地址
       async handleChooseAddress(){
         const [err,result]=await uni.chooseAddress()
          this.address=result
        },
        // 支付按钮
       async handlePay(){
            //#region 1 获取用户登录信息

            // 获取用户信息 uni.getUserProfile 需一开始点击获取
            const [err1,result1]=await uni.getUserProfile({
              desc:'用来给用户提供更好的体验'
            })

            // 获取code (临时登录凭据5分钟) uni.login
            const [err2,{code}]=await uni.login()

            // 调取登录接口
            const reult3=await this.$u.post('/users/wxlogin',{...result1,code})
            // console.log(reult3);
            const {token}=reult3.message
            //#endregion

            //#region 2 调用创建订单接口
            const orderParams={
                order_price:this.togglGoodsprice,
                consignee_addr:this.addressDetail,
                goods:this.checkedCarts.map((v)=>({
                    goods_id:v.goods_id,
                    goods_number:v.num,
                    goods_price:v.goods_price
                }))
            }
            const result4=await this.$u.post("/my/orders/create",orderParams,{Authorization:token})
            // console.log(result4);
            const{order_number}=result4.message

            //#endregion

            //#region 3 获取预支付参数
            const result5=await this.$u.post("/my/orders/req_unifiedorder",{order_number},{Authorization:token})
            // console.log(result5);
            const {pay}=result5.message
            //#endregion

            //#region 4 提交pay参数，发起微信支付
             await uni.requestPayment(pay)
            //#endregion



        }
     
    }
};
</script>

<style lang="scss">
.pay {
  padding-bottom: 111rpx;
}
.cart-item {
  display: flex;
  padding: 10rpx;
  border-bottom: 1rpx solid #ccc;

  .goods-img {
    flex: 2;

    display: flex;
    align-items: center;
    justify-content: center;
  }

  .goods-info {
    color: #666;
    flex: 3;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .goods-name {
    }

    .goods-price-num {
      display: flex;
      justify-content: space-between;
      .goods-price {
        color: #eb4450;
      }

      .goods-num-tool {
        width: 140rpx;
        height: 40rpx;

        display: flex;
        justify-content: flex-end;

        .goods-vnum {
        }
      }
    }
  }
}
.statistics {
  position: fixed;
  background-color: #fff;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: 100;
  border-top: 1rpx solid #ccc;
  display: flex;
  align-items: center;
  padding: 15rpx;
  .all-price {
    flex: 1;
    color: #666;
    text {
    }
  }

  .all-count {
  }
}
</style>